package com.lzz.bottomencapsulation.view.activity.needone;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.lzz.bottomencapsulation.mInterface.DownloadListener;
import com.lzz.bottomencapsulation.mvp.BasePresenterImpl;
import com.lzz.bottomencapsulation.utils.DownloadUtil;
import com.lzz.bottomencapsulation.utils.LUtils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class NeedOnePresenter extends BasePresenterImpl<NeedOneContract.View> implements
        NeedOneContract.Presenter {
    DownloadUtil downloadUtil;
    String filePath;//zip文件保存路径

    ArrayList<Bitmap> imgs;

    @Override
    public void downlodeZip(String zipUrl) {
        downloadUtil = new DownloadUtil(mView.getContext());
        downloadUtil.downloadFile(zipUrl, new DownloadListener() {
            @Override
            public void onStart() {
//                Looper.prepare();
//                TUtils.showShort(mView.getContext(), "开始下载");
//                Looper.loop();
            }

            @Override
            public void onProgress(int currentLength) {
                mView.showProgress(currentLength);
            }

            @Override
            public void onFinish(String localPath) {
                filePath = localPath;
//                TUtils.showShort(mView.getContext(), "下载结束");
            }

            @Override
            public void onFailure() {
//                TUtils.showShort(mView.getContext(), "下载错误");
            }
        });
    }

    @Override
    public void readZip() {
//        文件中有两种文件类型，  1、图片   2、配置文件

//        step1  不解压 直接读取zip文件中的图片，并显示出来
        LUtils.e(filePath);
        if (imgs == null) {
            imgs = new ArrayList<>();
        } else {
            imgs.clear();
        }

        try {
            imgs.addAll(readGuidePic(filePath));
        } catch (Exception e) {
            e.printStackTrace();
        }

        LUtils.e(imgs.toString());

        mView.setImageAdapter(adapter);
    }

    @Override
    public String getZipPath() {
        return filePath;
    }

    @Override
    public ArrayList<String> getImagesPath() {
        ArrayList<String> imagePath = new ArrayList<>();

        ZipFile zf = null;
        try {
            zf = new ZipFile(filePath);
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            ZipInputStream zin = new ZipInputStream(in);
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                    //Do nothing
                } else {
                    Log.i("tag", "file - " + ze.getName() + " : " + ze.getSize() + " bytes");

//                判断文件是否是png
                    String zePath = ze.getName();//文件的路径
                    String zeType = "";//文件类型
                    int zeTypeNum = zePath.lastIndexOf('.');//找到图片名称的位置
                    if (zeTypeNum != -1) {
                        zeType = zePath.substring(zeTypeNum);
                    }

                    if (zeType.equals(".png")) {
                        imagePath.add(zePath);
                    }

                }
            }
            zin.closeEntry();
            return imagePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imagePath;
    }


    public static ArrayList<Bitmap> readGuidePic(String file) throws Exception {
        ArrayList<Bitmap> bitmaps = new ArrayList<>();
        String fileName = file.substring(file.length() - 7, file.length() - 4);
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //Do nothing
            } else {
                Log.i("tag", "file - " + ze.getName() + " : " + ze.getSize() + " bytes");

//                判断文件是否是png
                String zePath = ze.getName();//文件的路径
                String zeType = "";//文件类型
                int zeTypeNum = zePath.lastIndexOf('.');//找到图片名称的位置
                if (zeTypeNum != -1) {
                    zeType = zePath.substring(zeTypeNum);
                }

                if (zeType.equals(".png")) {
                    InputStream is = zf.getInputStream(ze);
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    if (bitmap != null) bitmaps.add(bitmap);
                }

            }
        }
        zin.closeEntry();
        return bitmaps;
    }

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return imgs.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        // 该方法的返回的View就是代表了每个列表项
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 创建一个ImageView
            ImageView imageView = new ImageView(mView.getContext());
            imageView.setImageBitmap(imgs.get(position));
            // 设置ImageView的缩放类型
//            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            // 为imageView设置布局参数
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                    .MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return imageView;
        }
    };


}
